<?php
include('/home/wwwroot/football.laulei.com/protected/extensions/simple_html_dom.php');
//只获取并更新北京单场当前期的威廉希尔的最终赔率
class GetWilliamhillResultOddsCommand extends CConsoleCommand  
{
    public function run($args) {
    	$issueArry=array();
        $html = file_get_html('http://trade.500.com/bjdc/');
		$select = $html->find('select[id=expect_select]', 0);
	    $option = $select->find('option', 0);
    	$issue=$option->value;
    	$issueArrTmp = explode("|",$issue);
    	array_push($issueArry, $issueArrTmp[0]);
		$html->clear();
		unset($html);
    	/*
    	$issueArry=array();
        $html = file_get_html('http://trade.500.com/bjdc/');
		$select = $html->find('select[id=expect_select]', 0);
	    foreach($select->find('option') as $option){
	    	$issue=$option->value;
	    	$issueArrTmp = explode("|",$issue);
	    	array_push($issueArry, $issueArrTmp[0]);
	    }
		$html->clear();
		unset($html);
		
		
		$issueArry=array();
		$beginIssue='101203';//起始期号
		$endIssue='140101';//结束期号
		array_push($issueArry, $beginIssue);
		while($beginIssue!=$endIssue){
			$issuePartYear = intval(substr($beginIssue, 0, 2));
			$issuePartMonth = intval(substr($beginIssue, 2, 2));
			$issuePartDay = intval(substr($beginIssue, 4, 2));
			printf("%s\t%s\t%s\n", $issuePartYear, $issuePartMonth, $issuePartDay);
			$issuePartDay=$issuePartDay+1;
			if($issuePartDay>20){
				$issuePartDay=1;
				$issuePartMonth=$issuePartMonth+1;
			}
			if($issuePartMonth>12){
				$issuePartMonth=1;
				$issuePartYear=$issuePartYear+1;
			}

			$beginIssue=sprintf("%02d%02d%02d", $issuePartYear, $issuePartMonth, $issuePartDay);
			array_push($issueArry, $beginIssue);
			printf("%s\t%s\t%s\n", $issuePartYear, $issuePartMonth, $issuePartDay);
			printf("%s\n", $beginIssue);
		}
		*/
		foreach ($issueArry as $key => $value) {
			try{
				$urlEveryIssue='http://zc.trade.500.com/bjdc/?expect='.$value;
				printf("%s\n", $urlEveryIssue);
		    	$htmlIssue = file_get_html($urlEveryIssue);
		    	if(!isset($htmlIssue)){
		    		continue;
		    	}
				foreach($htmlIssue->find('tr[class=vs_lines]') as $game) {
					try{
						//比赛编号
						$gameid=trim($game->fid);
						$gameInfoObj=GameInfo::model()->find(array('condition'=>'gameid=:gameid','params'=>array(':gameid'=>$gameid)));
						if(isset($gameInfoObj)){
							$oddsUrl=sprintf("http://odds.500.com/fenxi/ouzhi-%s-show-2?ctype=1", $gameid);
							$htmlOdds = file_get_html($oddsUrl);
					    	if(!isset($htmlOdds)){
					    		continue;
					    	}
	    					$tmpTR=$htmlOdds->find("tr[id=tr2_293]", 0);
				    		if(!isset($tmpTR)){
				    			$htmlOdds->clear();
								unset($htmlOdds);
				    			continue;
				    		}
				    		$gameInfoObj->euroodds3w=$tmpTR->find("td", 0)->plaintext;
				    		$gameInfoObj->euroodds1w=$tmpTR->find("td", 1)->plaintext;
				    		$gameInfoObj->euroodds0w=$tmpTR->find("td", 2)->plaintext;
							$gameInfoObj->updatetime=date("Y-m-d H:i:s");
							if ($gameInfoObj->update()) {
								printf("end find gameid [%s], game over, update it!\n", $gameid);
							}
							else{
								printf("end find gameid [%s], update fail!\n", $gameid);
							}
							$htmlOdds->clear();
							unset($htmlOdds);
						}
						else{
							printf("end find gameid [%s], it's not set\n", $gameid);
						}
					}
					catch(Exception $ex){

					}
				}
			}
			catch(Exception $ex){

			}
			$htmlIssue->clear();
			unset($htmlIssue);
		}
    }
}